<?php

namespace Drawping\Amap\Yuntu;

use Drawping\Amap\AbstractAPI;

class Search extends AbstractAPI
{
    const API_AROUND = 'http://yuntuapi.amap.com/datasearch/around';

    /**
     * 周边检索
     * @param $center 中心点
     * @param array $filter 过滤条件
     * @param int $radius 查询半径
     * @param string $keywords 搜索关键词
     * @param array $sortrule 排序 如array('_distance', 1)
     * @param int $limit 每页记录数
     * @param int $page 当前页数
     * @return bool|array 当报错时返回false
     */
    function around($center, $filter = [], $radius = 3000, $keywords = '', $sortrule = ['_distance', 1], $limit = 100, $page = 1){
        $filter_format = [];
        array_walk($filter, function($v, $k) use (&$filter_format){
            $filter_format[] = $k.':'.(is_array($v) ? '['.implode(',', $v).']' : $v);
        });
        $query = [
            'key' => $this->config['app_key'],
            'tableid' => $this->tableid,
            'keywords' =>$keywords,
            'center' => $center,
            'radius' => $radius,
            'filter' => implode('+', $filter_format),
            'sortrule' => implode(':', $sortrule),
            'limit' => $limit,
            'page' =>$page,
        ];

        $response = $this->http->request(
            'GET',
            self::API_AROUND,
            [
                'query' => $this->signature($query)
            ]
        );

        $r = $this->parseJSON($response);
        return $r ? $r->datas : false;
    }
}
